<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { getPlanListApi, deletePlanApi, putPlanApi } from '@/api/check/plan'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref, reactive } from 'vue'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { PlanType } from '@/api/check/plan/types'
import { ElButton } from 'element-plus'

const ids = ref<string[] | number[]>([])

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await getPlanListApi({
      page: unref(currentPage),
      limit: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data,
      total: res.count
    }
  },
  fetchDelApi: async () => {
    const res = await deletePlanApi(unref(ids))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList, getElTableExpose, delList } = tableMethods

const searchParams = ref({
  status: '未处理'
})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'selection',
    fixed: true,
    type: 'selection',
    width: 80,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'index',
    label: t('common.index'),
    type: 'index',
    width: 80,
    fixed: true,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'part_no',
    label: '材料/零件图号',
    minWidth: 120,
    fixed: true
  },
  {
    field: 'part_name',
    label: '材料/零件名称',
    minWidth: 120,
    fixed: true
  },
  {
    field: 'form_no',
    label: '单据编号',
    minWidth: 120,
    fixed: true
  },
  {
    field: 'plan_type',
    label: '待办类型',
    minWidth: 120,
    fixed: true,
    search: {
      component: 'Select',
      componentProps: {
        options: [
          // {
          //   value: '驳回'
          // },
          {
            value: '母本异常'
          }
          // {
          //   value: '升版'
          // }
        ]
      }
    }
  },
  {
    field: 'content',
    minWidth: 160,
    label: '内容',
    search: {
      hidden: true
    }
  },
  {
    field: 'status',
    label: '状态',
    minWidth: 100,
    search: {
      component: 'Select',
      componentProps: {
        options: [
          {
            value: '已处理'
          },
          {
            value: '未处理'
          }
        ]
      },
      value: '未处理'
    },
    form: {
      hidden: false
    }
  },
  {
    field: 'create_user',
    label: '创建人',
    minWidth: 100,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'create_datetime',
    label: '创建时间',
    minWidth: 180,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    }
  },
  {
    field: 'action',
    label: t('common.action'),
    minWidth: 100,
    search: {
      hidden: true
    },
    form: {
      hidden: true
    },
    table: {
      slots: {
        default: (data: any) => {
          return (
            <>
              <ElButton onClick={() => save(data.row)}>确认</ElButton>
            </>
          )
        }
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

// const dialogVisible = ref(false)
// const dialogTitle = ref('')

// const currentRow = ref<PlanType | null>(null)
// const actionType = ref('')

// const AddAction = () => {
//   dialogTitle.value = t('common.add')
//   currentRow.value = null
//   dialogVisible.value = true
//   actionType.value = 'add'
// }

// const delLoading = ref(false)

// const delData = async (row: PlanType | null) => {
//   const elTableExpose = await getElTableExpose()
//   ids.value = row ? [row.id] : elTableExpose?.getSelectionRows().map((v: PlanType) => v.id) || []
//   delLoading.value = true
//   await delList(unref(ids).length).finally(() => {
//     delLoading.value = false
//   })
// }

// const action = (row: PlanType, type: string) => {
//   dialogTitle.value = t(type === 'edit' ? 'common.edit' : 'common.detail')
//   actionType.value = type
//   currentRow.value = row
//   dialogVisible.value = true
// }

// const writeRef = ref<ComponentRef<typeof Write>>()

const saveLoading = ref(false)

const save = async (row: PlanType) => {
  if (row) {
    saveLoading.value = true
    try {
      const res = await putPlanApi(row)
      if (res) {
        console.log('res', res)
        getList()
      }
    } finally {
      saveLoading.value = false
    }
  }
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <!-- <div class="mb-10px">
      <ElButton type="primary" @click="AddAction">{{ t('common.add') }}</ElButton>
      <ElButton :loading="delLoading" type="danger" @click="delData(null)">
        {{ t('common.del') }}
      </ElButton>
    </div> -->

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
      :fit="true"
    />
  </ContentWrap>

  <!-- <Dialog v-model="dialogVisible" :title="dialogTitle" width="700px" max-height="340px">
    <Write
      v-if="actionType !== 'detail'"
      ref="writeRef"
      :current-row="currentRow"
      :form-schema="allSchemas.formSchema"
    />
    <template #footer>
      <ElButton
        v-if="actionType !== 'detail'"
        type="primary"
        :loading="saveLoading"
        @click="save"
      >
        {{ t('common.save') }}
      </ElButton>
      <ElButton @click="dialogVisible = false">{{ t('common.close') }}</ElButton>
    </template>
  </Dialog> -->
</template>
